package com.lovo.Atm;

import java.sql.*;

public class JDUtil {

    public Connection getConnection() {
        Connection con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/emp?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "qq1137545199");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    //关闭三个
    public void close(Connection con, Statement statement, ResultSet res) {
        if (res != null) {
            try {
                res.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    //关闭两个
    public void close(Connection con, Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 根据用户名和密码查询用户对象
     *
     * @param name
     * @param password
     * @return
     */
    public User getByUsernameAndPwd(String name, String password) {
        Connection con = getConnection();
        Statement statement = null;
        ResultSet res = null;
        User user = null;
        String sql = "select * from t_user where u_name='" + name + "' and u_password='" + password + "'";
        try {
            statement = con.createStatement();
            res = statement.executeQuery(sql);
            if (res.next()) {
                user = new User();
                user.setId(res.getInt("u_id"));
                user.setName(res.getString("u_name"));
                user.setPassword(res.getString("u_password"));
                user.setBlance(res.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, res);
        }
        return user;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public User getById(int id) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet res = null;
        User user = null;
        String sql = "select * from t_user where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            res = statement.executeQuery();
            if (res.next()) {
                user = new User();
                user.setId(res.getInt("u_id"));
                user.setName(res.getString("u_name"));
                user.setPassword(res.getString("u_password"));
                user.setBlance(res.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, res);
        }
        return user;
    }

    /**
     * 根据用户名查询用户对象
     *
     * @param name
     * @return
     */
    public User getByUsername(String name) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet res = null;
        User user = null;
        String sql = "select * from t_user where u_name=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, name);
            res = statement.executeQuery();
            if (res.next()) {
                user = new User();
                user.setId(res.getInt("u_id"));
                user.setName(res.getString("u_name"));
                user.setPassword(res.getString("u_password"));
                user.setBlance(res.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, res);
        }
        return user;
    }

    /**
     * 根据User对象中的数据进行修改  改存款 改密码
     *
     * @param user
     */
    public void update(User user) {
        // 1. 创建连接
        Connection con = getConnection();
        PreparedStatement statement = null;
        String sql = "update t_user set u_password=?,u_blance=? where u_id=?";
        try {
            // 2. 获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1, user.getPassword());
            statement.setObject(2, user.getBlance());
            statement.setObject(3, user.getId());
            // 3. 执行SQL语句
            int row = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
            close(con, statement);
        }
    }

    /**
     * 转账事务
     */
    public void transfer(User user, User otherUser) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        String sql = "update t_user set u_blance =? where u_name =?";
        try {
            con.setAutoCommit(false);
            statement = con.prepareStatement(sql);
            statement.setObject(1, user.getBlance());
            statement.setObject(2, user.getName());
            statement.executeUpdate();

            statement = con.prepareStatement(sql);
            statement.setObject(1, otherUser.getBlance());
            statement.setObject(2, otherUser.getName());
            statement.executeUpdate();
            con.commit();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            close(con, statement);
        }
    }

}
